package com.ccm.demo.rediscache.service;

import com.ccm.demo.rediscache.dao.HelloWorldDao;
import com.ccm.demo.rediscache.entity.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@CacheConfig(cacheNames = "login")
public class HelloWorld2Service {

	@Resource
	private HelloWorldDao helloWorldDao;

	@Cacheable(key = "'login_'+#id")
	public User queryUser(Integer id) {
		User user = helloWorldDao.queryUser(id);
		System.out.println("数据库中取得user:"+user);
		return user;
	}
	//beforeInvocation默认false,在方法调用之后清除缓存
	//为true时在方法调用之前清除缓存
	@CacheEvict(beforeInvocation = true,key = "'login_'+#id")
	public void delUser(Integer id){
		helloWorldDao.delUser(id);
	}

	@CacheEvict(beforeInvocation = true,key = "'login_'+#user.id")
	public void updateUser(User user) {
		helloWorldDao.updateUser(user);
	}
}
